#include "Includes.h"
#include "DEV_Timer.h"

TICK_TIMER_ST ttimer[MAX_TIMERID]=TTIMER_DEF;

int TTimer_Init(void)
{
    //测试函数初始化
    TTimer_Start(DEV_TIMER_TEST_TIMERID);
    return 0;
}

void TTimer_Start(U32 timerid)
{
    ttimer[timerid].tick=OppTickCountGet();
}

void TTimer_Stop(U32 timerid)
{
    ttimer[timerid].tick=0;
}

int TTimer_IsRunning(U32 timerid)
{
    if(ttimer[timerid].tick==0)
        return 0;
    else
        return 1;
}

//0.未运行,1剩余时间
U32 TTimer_RemainTime(U32 timerid)
{
    if(ttimer[timerid].tick==0)
        return 0;
    return OppTickCountGet()-ttimer[timerid].tick;
}


void TTimer_Loop(void)
{
    int i=0;
    
    for(i=0;i<MAX_TIMERID;i++)
    {
        if(ttimer[i].tick==0)
            continue;
        
        if(OppTickCountGet()-ttimer[i].tick > ttimer[i].expires)
        {
            if(ttimer[i].cycle==1)
                ttimer[i].tick=OppTickCountGet();
            else
                ttimer[i].tick=0;
            if(ttimer[i].pfunc!=NULL)
                ttimer[i].pfunc(ttimer[i].pParameter);
        }
    }
}


